$(function(){
	var isAdd = true;
	
	$(document).ready(function(){
		$("#authAdd").click(function(){
			$("#ml").removeAttr("checked");
			$("#cd").removeAttr("checked");
			$("#an").removeAttr("checked");
		});
		$("input[name=authType]").click(function(){
			var authType = $(this).val();
			if(authType == 0){
				$("#authUrl").val("");
				$("#authPerms").val("");
				$("#authUrl").attr("disabled","disabled");
				$("#authPerms").attr("disabled","disabled");
				$("#orderNum").removeAttr("disabled");
				$("#parentId").val(0);
				$("#parentId").attr("disabled","disabled");
			}
			if(authType == 1){
				$("#authUrl").removeAttr("disabled");
				$("#authPerms").attr("disabled","disabled");
				$("#orderNum").removeAttr("disabled");
				$("#parentId").removeAttr("disabled");
			}
			if(authType == 2){
				$("#authUrl").val("");
				$("#orderNum").val(0);
				$("#authPerms").removeAttr("disabled");
				$("#parentId").removeAttr("disabled");
				$("#authUrl").attr("disabled","disabled");
				$("#orderNum").attr("disabled","disabled");
			}
		});
	});
	
	$("#addAuthForm").validate({
		errorElement: 'span',
		errorPlacement: function(error, element) {  
		    error.appendTo(element.parent());  
		},
		rules:{
			authType:"required",
			authName:"required",
		},
		messages:{
			authType:"请选择菜单类型",
			authName:"请输入菜单名称",
		},
		submitHandler : function(form) {  //验证通过后的执行方法
            //当前的form通过ajax方式提交（用到jQuery.Form文件）
			var url = isAdd ? "addAuth" : "updateAuth";
			var authName = $("input[name=authName]").val();
			var authUrl = $("input[name=authUrl]").val();
			var authParentId = $("select[name=authParentId]").val();
			var authType = $("input[name='authType']:checked").val();
			var authPerms = $("input[name=authPerms]").val();
			var orderNum = $("input[name=orderNum]").val();
			var authId = $("input[name=authId]").val();
			if(isAdd){
				$.post(url, {authName: authName,authUrl: authUrl,authParentId: authParentId,
					authType: authType,authPerms: authPerms,orderNum: orderNum}, function(data){
					if(data.code == 200){
						layer.msg(data.msg, {icon : 1,time : 1500,});
						setTimeout(function(){
							$("#modal-edit").modal('hide');
							$('#table').bootstrapTable("refresh");
						},2000);
					}else{
						layer.msg(data.msg, {icon : 2,time : 1500,});
					}
				},"json");
			}else{
				$.post(url, {authName: authName,authUrl: authUrl,authParentId: authParentId,
					authType: authType,authPerms: authPerms,orderNum: orderNum,authId: authId}, function(data){
					if(data.code == 200){
						layer.msg(data.msg, {icon : 1,time : 1500,});
						setTimeout(function(){
							$("#modal-edit").modal('hide');
							$('#table').bootstrapTable("refresh");
						},2000);
					}else{
						layer.msg(data.msg, {icon : 2,time : 1500,});
					}
				},"json");
			}
        },
        
	});
	
	//批量删除
	$("#deleteBtn").click(function() {
		var ids = getIdSelections();
		if (ids == "") {
			layer.msg("未选择数据");
		} else {
			layer.confirm('确定要删除所选数据吗？',{btn: ['确定','取消']},function (){	
				$.post("deleteAuthById", {
					pIds: JSON.stringify(ids)
				}, function(data){
					if(data.code == 200){
						layer.msg(data.msg, {icon : 1,time : 1500,});
					}else{
						layer.msg(data.msg, {icon : 2,time : 1500,});
					}
					$('#table').bootstrapTable("refresh");
					$('#table').bootstrapTable('remove', { field: 'authId', values: ids});
				},"json");
				return true;
			});
		}
	});
	
	$(".btn-primary").click(function (){
		$(this).attr("disabled",true);
		 $("#addAuthForm").submit(); 
	    setTimeout("$('.btn-primary').removeAttr('disabled')",3000);
	});

	$("#edit").click(function() {
		isAdd = false;
		var ids = getAllChecked();
		if (ids.length == 1) {
			var auth = getSelections();
			console.log(auth)
			$("input[name=authName]").val(auth[0].auth_name);
			$("select[name=authParentId]").val(auth[0].auth_parent_id);
			$("input[name=authPerms]").val(auth[0].auth_perms);
			if(auth[0].auth_type == 0){
				$("#ml").prop('checked',true);
			}
			if(auth[0].auth_type == 1){
				$("#cd").prop('checked',true);
			}
			if(auth[0].auth_type == 2){
				$("#an").prop('checked',true);
			}
			$("input[name=authUrl]").val(auth[0].auth_url);
			$("input[name=orderNum]").val(auth[0].order_num);
			$("#authId").val(auth[0].auth_id);
			$('#modal-edit').modal('toggle');
		}else if(ids.length == 0){
			layer.msg("未选择数据");
		}else{
			layer.msg("请勿勾选多个");
		}
	});
	
	function getAllChecked (){
  		var ids = [];
  		$("input[type=checkbox]").each(
  			function (){
  				if(this.checked){
  					ids.push($(this).val());
  				}
  			}
  		);
  		return ids;
  	} 
	
	/*
	 * 得到所有选中行
	 */
	function getIdSelections() {
		return $.map($('#table').bootstrapTable('getSelections'), function(row) {
			return row.auth_id;
		});
	}
	
	function getSelections() {
		return $.map($('#table').bootstrapTable('getSelections'), function(row) {
			return row;
		});
	}
	/*清空新增时的表单*/
	function clear(obj){
		$(obj).click(function(){
			location.href = "../a/sysauth"
		});
	}
	/*新增弹框×号*/
	clear(".close");
	/*取消按钮*/
	clear(".closeBtn");
});
